VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "RectangleSprite"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
' ************************************************
' Moving rectangle sprite.
' ************************************************
Option Explicit

Dim Wid As Integer      ' Width.
Dim Hgt As Integer      ' Height.
Dim Cx As Integer       ' Position of center.
Dim Cy As Integer
Dim Theta As Single     ' Orientation.
Dim Vx As Integer       ' Velocity.
Dim Vy As Integer
Dim Vtheta As Single    ' Angular velocity.
Dim Clr As Long         ' Color.
' ************************************************
' Draw the rectangle on the indicated picture box.
' ************************************************
Public Sub DrawSprite(pic As PictureBox)
Const PI = 3.14159
Const PI_OVER_2 = PI / 2

Dim wx As Single
Dim wy As Single
Dim hx As Single
Dim hy As Single
Dim pts(1 To 4) As POINTAPI
Dim status As Long
Dim newpen As Long
Dim oldpen As Long
Dim newbrush As Long
Dim oldbrush As Long

    ' Compute vectors parallel to the axes.
    wx = Wid * Cos(Theta)
    wy = Wid * Sin(Theta)
    hx = Hgt * Cos(Theta + PI_OVER_2)
    hy = Hgt * Sin(Theta + PI_OVER_2)
    
    pts(1).x = Cx + (wx + hx) / 2
    pts(1).y = Cy + (wy + hy) / 2
    pts(2).x = pts(1).x - hx
    pts(2).y = pts(1).y - hy
    pts(3).x = pts(2).x - wx
    pts(3).y = pts(2).y - wy
    pts(4).x = pts(3).x + hx
    pts(4).y = pts(3).y + hy
    
    newpen = CreatePen(PS_SOLID, 0, Clr)
    oldpen = SelectObject(pic.hdc, newpen)
    newbrush = CreateSolidBrush(Clr)
    oldbrush = SelectObject(pic.hdc, newbrush)
    
    status = Polygon(pic.hdc, pts(1), 4)
    
    newpen = SelectObject(pic.hdc, oldpen)
    status = DeleteObject(newpen)
    newbrush = SelectObject(pic.hdc, oldbrush)
    status = DeleteObject(newbrush)
End Sub

' ************************************************
' Initialize the rectangle.
' ************************************************
Public Sub InitializeSprite(w As Integer, h As Integer, x As Integer, y As Integer, angle As Single, dx As Integer, dy As Integer, dangle As Single, c As Long)
    Wid = w
    Hgt = h
    Cx = x
    Cy = y
    Theta = angle
    Vx = dx
    Vy = dy
    Vtheta = dangle
    Clr = c
End Sub


' ************************************************
' Add the velocity components to the sprite's
' position components.
' ************************************************
Public Sub MoveSprite(xmax As Integer, ymax As Integer)
    Cx = Cx + Vx
    Cy = Cy + Vy
    
    If Cx < 0 Then
        Cx = -Cx
        Vx = -Vx
    ElseIf Cx > xmax Then
        Cx = 2 * xmax - Cx
        Vx = -Vx
    End If
    If Cy < 0 Then
        Cy = -Cy
        Vy = -Vy
    ElseIf Cy > ymax Then
        Cy = 2 * ymax - Cy
        Vy = -Vy
    End If
    
    Theta = Theta + Vtheta
End Sub


